package com.example.hosptial.newController;

import com.example.hosptial.service.DeepSeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Map;

@RestController
@RequestMapping("/api/deepseek")
@CrossOrigin
public class DeepSeekController {

    @Autowired
    private DeepSeekService deepSeekService;

    @GetMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter streamChat(@RequestParam("message") String message) {
        SseEmitter emitter = new SseEmitter(-1L); // No timeout
        
        if (message == null || message.trim().isEmpty()) {
            try {
                emitter.send("Message cannot be empty");
                emitter.complete();
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
            return emitter;
        }

        // 在新线程中处理流式响应
        Thread thread = new Thread(() -> {
            deepSeekService.streamResponse(message, emitter);
        });
        thread.start();

        return emitter;
    }

    @GetMapping("/health")
    public ResponseEntity<String> healthCheck() {
        try {
            return ResponseEntity.ok("DeepSeek service is available");
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("DeepSeek service is not available: " + e.getMessage());
        }
    }
} 